package com.weipu.common.okhttp.request;

import java.util.Map;

import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.weipu.common.constants.Constant.HttpMethod;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.util.SecurityUtil;

/**
 * Request 构造基类
 * @author huangfei
 *
 */
public abstract class OkHttpRequest {
	protected static final String TAG = "OkHttpRequest";

	protected String url;
	protected Object tag;
	protected Map<String, String> params;
	protected Map<String, String> headers;

	Request.Builder builder = new Request.Builder();
	
	public OkHttpRequest(String url, Object tag, Map<String, String> headers){
		this.url=url;
		this.tag=tag;
		this.headers=headers;
		if(null==url||"".equals(url)){
			throw new IllegalArgumentException("Url can not be null");
		}
		setRequestBuilder();
	}

	/**
	 * 获取权限帐号
	 * 
	 * @return
	 */
	public String getCredential() {
		String credential;
		try {
			credential = Credentials
					.basic(SecurityUtil
							.aesEncrypt(ServerConstant.Parameter.PARAMETER_DEV_AUTHENTICATE_ACCOUNT),
							SecurityUtil
									.aesEncrypt(ServerConstant.Parameter.PARAMETER_DEV_AUTHENTICATE_PASSWORD));
		} catch (Exception e) {
			credential = Credentials
					.basic(ServerConstant.Parameter.PARAMETER_DEV_AUTHENTICATE_ACCOUNT,
							ServerConstant.Parameter.PARAMETER_DEV_AUTHENTICATE_PASSWORD);
		}
		return credential;
	}

	/**
	 * 设置请求对象的Builder参数
	 * @param url 请求Url
	 * @param tag 请求Tag
	 * @param headers 请求头
	 */
	private void setRequestBuilder() {
		builder.url(url).tag(tag);
		setBuildHeaders(headers);
	}

	/**
	 * 构建请求对象
	 * @param requestBody 请求Body
	 * @param method 请求方法
	 * @return
	 */
	protected Request buildRequest(RequestBody requestBody,
			HttpMethod method){
		switch (method) {
		case GET:
			builder.build();
			break;
		case POST:
			builder.post(requestBody).build();
			break;
		case PUT:
			builder.put(requestBody).build();
			break;
		case DELETE:
			builder.delete(requestBody).build();
			break;

		default:
			break;
		}
		return builder.post(requestBody).build();
	}


	/**
	 * 设置请求的属性
	 * 
	 * @param url
	 *            请求地址
	 * @param params
	 *            请求参数
	 * @param headers
	 *            请求Header
	 * @param tag
	 *            请求Tag
	 */
	public void setBuild(String url, Map<String, String> headers, Object tag) {
		if (null != url && !"".equals(url)) {
			builder.url(url);
		}
		if (null != tag && !"".equals(tag)) {
			builder.tag(tag);
		}
		if (headers != null && !headers.isEmpty()) {
			Headers.Builder headerBuilder = new Headers.Builder();
			headerBuilder.add("Authorization", getCredential());
			for (String key : headers.keySet()) {
				headerBuilder.add(key, headers.get(key));
			}
			builder.headers(headerBuilder.build());
		}

	}

	/**
	 * 设置请求Tag
	 * 
	 * @param tag
	 */
	public void setBuildTag(Object tag) {
		if (null != tag && !"".equals(tag)) {
			builder.tag(tag);
		}
	}

	/**
	 * 设置请求Url
	 * 
	 * @param url
	 */
	public void setBuildUrl(String url) {
		if (null != url && !"".equals(url)) {
			builder.url(url);
		}
	}

	/**
	 * 设置请求Header
	 * 
	 * @param headers
	 *            headers对象
	 */
	public void setBuildHeaders(Map<String, String> headers) {
		if (headers != null && !headers.isEmpty()) {
			Headers.Builder headerBuilder = new Headers.Builder();
			headerBuilder.add("Authorization", getCredential());
			for (String key : headers.keySet()) {
				headerBuilder.add(key, headers.get(key));
			}
			builder.headers(headerBuilder.build());
		}

	}
	/**
	 * 获取最终的请求Builder对象
	 * @return
	 */
	protected Request.Builder getRequestBuilder(){
		return builder;
	}

}
